package com.liik.bomb.screen.action;

import com.liik.game.mvc.object.Menu.Item;
import com.liik.game.mvc.tiled.TiledGameModel;
import com.liik.game.mvc.tiled.object.Entity;
import com.liik.game.mvc.tiled.object.MovableEntity;
import com.liik.game.mvc.tiled.object.Tile;
import com.liik.game.mvc.tiled.object.Tile.Type;

public class BombGameModel extends TiledGameModel {
	public static final int LEVEL_TILE_WIDTH = 30;
	public static final int LEVEL_TILE_HEIGHT = 30;
	public static final int MAX_MENU_ENTRIES = 10;
	public static final int MAX_BOMBS = 10;
	public static final int MAX_ENEMIES = 10;

	public BombEntity[] bombs = new BombEntity[MAX_BOMBS];
	public EnemyEntity[] enemies = new EnemyEntity[MAX_ENEMIES];
	public Entity[] allEntities = new Entity[MAX_BOMBS + MAX_ENEMIES];
	public BombermanEntity hero = new BombermanEntity();

	public BombGameModel() {
		super(LEVEL_TILE_WIDTH, LEVEL_TILE_HEIGHT, MAX_MENU_ENTRIES);
		initMenu();
	}

	private void initMenu() {
		getMenu().addItem(new Item(BombGameController.MENU_ID_PAUSE, 110, 10, 90, 90, "Pause"));
		getMenu().addItem(new Item(BombGameController.MENU_ID_EXIT, 210, 10, 90, 90, "Exit"));
	}
	
	public void start() {
		initBombs(0);
		initEnemies(MAX_BOMBS);
		initLevel();
		System.gc();
	}

	private void initBombs(int offset) {
		for (int i = 0; i < MAX_BOMBS; ++i) {
			bombs[i] = new BombEntity();
			allEntities[offset + i] = bombs[i];
		}
	}

	private void initEnemies(int offset) {
		for (int i = 0; i < MAX_ENEMIES; ++i) {
			enemies[i] = new EnemyEntity();
			allEntities[offset + i] = enemies[i];
		}
		enemies[0].initialize(5.5f, 5.5f, .01f, 0);
		enemies[1].initialize(8.5f, 3.5f, .03f, 0);
		enemies[2].initialize(1.5f, 17.5f, .05f, 0);
		enemies[3].initialize(11.5f, 17.5f, .03f, 0);
		enemies[4].initialize(4.5f, 13.5f, .06f, 0);
		enemies[5].initialize(3.5f, 22.5f, .01f, 0);
		enemies[6].initialize(9.5f, 10.5f, .04f, 0);
		enemies[7].initialize(2.5f, 21.5f, .02f, 0);
	}

	private void initLevel() {
		// set start position of hero
		hero.initialize(.5f, .5f, 5, 3, 2);
		// fill everything with earth and wall
		Tile[][] tiles = getTiles();
		for (int i = 0; i < LEVEL_TILE_WIDTH; ++i) {
			for (int j = 0; j < LEVEL_TILE_HEIGHT; ++j) {
				tiles[i][j] = new Tile();
				if (i % 2 == 1 && j % 2 == 1) {
					tiles[i][j].type = Type.Wall;
				} else {
					tiles[i][j].type = Type.Earth;
				}
				tiles[i][j].subtype = (int) (Math.random() * 4);
			}
		}
		// prepare empty start space
		tiles[(int) hero.x][(int) hero.y].type = Type.Empty;
		tiles[(int) hero.x + 1][(int) hero.y].type = Type.Empty;
		tiles[(int) hero.x + 2][(int) hero.y].type = Type.Empty;
		tiles[(int) hero.x + 3][(int) hero.y].type = Type.Empty;
		tiles[(int) hero.x][(int) hero.y + 1].type = Type.Empty;
		tiles[(int) hero.x][(int) hero.y + 2].type = Type.Empty;
		tiles[(int) hero.x][(int) hero.y + 3].type = Type.Empty;

		// prepare empty start space for all enemies
		for (int i = 0; i < enemies.length; ++i) {
			EnemyEntity enemy = enemies[i];
			if (enemy != null && enemy.isActive()) {
				tiles[(int) enemy.x][(int) enemy.y].type = Type.Empty;
				tiles[(int) enemy.x + 1][(int) enemy.y].type = Type.Empty;
				tiles[(int) enemy.x - 1][(int) enemy.y].type = Type.Empty;
			}
		}
	}

	@Override
	public MovableEntity getHero() {
		return hero;
	}

	@Override
	public Entity[] getAllEntities() {
		return allEntities;
	}

	public BombEntity getInactiveBomb() {
		for (int i = 0; i < MAX_BOMBS; ++i) {
			if (!bombs[i].isActive()) {
				return bombs[i];
			}
		}
		return null;
	}
}
